package demo.practice.dp;

public class P91 {

    public static void main(String[] args) {
        P91 p91 = new P91();
        int i = p91.numDecodings("2101");
        System.out.println(i);
    }

    public int numDecodings(String s) {

        if (s == null || s.length() == 0) {
            return 0;
        }
        int n = s.length();
        int dp[] = new int[n + 1];
        dp[0] = 1;//前0个元素有一种解法
        char[] c = s.toCharArray();

        for (int i = 1; i <= n; i++) {
            dp[i] = 0;
            int t = c[i - 1] - '0';
            if (t >= 1 && t <= 9) {
                //i-1这位 为1-9
                dp[i] += dp[i - 1];
            }

            if (i >= 2) {
                //两位以上
                t= (c[i-2]-'0')*10 + (c[i-1]-'0');
                if(t>=10 &&t<=26){
                    dp[i] += dp[i - 2];
                }
            }
        }
        return dp[n];
    }
}
